package com.youfan.control;

import com.alibaba.fastjson.JSONObject;
import com.youfan.entity.Brand;
import com.youfan.page.CommonPage;
import com.youfan.re.Result;
import com.youfan.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

@RestController
@RequestMapping(value = "brand")
public class BrandControl {

    @Value("${picturepath}")
    private String picturepath;

    @Autowired
    private BrandService brandService;

    @RequestMapping(method = RequestMethod.POST,value = "add")
    @ResponseBody
    public Result add(@RequestParam("logofile") MultipartFile logoFile,
                      @RequestParam("logobigfile") MultipartFile logoBigFile,
                     @RequestParam(value = "brandform" ) String brandform)
    {
        try {
            if (logoFile.isEmpty()||logoBigFile.isEmpty()) {
                throw new Exception("上传文件不能为空");
            }
            Brand youfanBrand = JSONObject.parseObject(brandform, Brand.class);
            /**
             * 品牌logo图片处理
             */
            String oldlogoFileName = logoFile.getOriginalFilename();
            String path = picturepath+"/brand/logo";

            File file = new File(path);
            if (!file.isDirectory()){
                file.mkdirs();
            }
            String newName = UUID.randomUUID().toString() +oldlogoFileName.substring(oldlogoFileName.indexOf("."), oldlogoFileName.length());
            File newFile = new File(file, newName);
            logoFile.transferTo(newFile);
            youfanBrand.setLogo("/brand/logo/"+newName);

            /**
             * 品牌logobig图片处理
             */
            oldlogoFileName = logoBigFile.getOriginalFilename();
            path = picturepath+"/brand/biglogo";

            file = new File(path);
            if (!file.isDirectory()){
                file.mkdirs();
            }
            newName = UUID.randomUUID().toString() +oldlogoFileName.substring(oldlogoFileName.indexOf("."), oldlogoFileName.length());
            newFile = new File(file, newName);
            logoBigFile.transferTo(newFile);
            youfanBrand.setBigPic("/brand/biglogo/"+newName);
            brandService.insertOne(youfanBrand);
            return Result.success("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("添加失败");
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "list")
    @ResponseBody
    public Result list(@RequestBody Brand youfanBrand){
        try {
            if (youfanBrand.getPageNum() == null) {
                youfanBrand.setPageNum(1);
            }
            if (youfanBrand.getPageSize() == null) {
                youfanBrand.setPageSize(5);
            }
            CommonPage<Brand> resultdata = brandService.queryByCondtion(youfanBrand);

            return Result.success(resultdata, "查询成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败"+e.getMessage());
        }
    }

    @RequestMapping(method = RequestMethod.POST,value = "findby")
    @ResponseBody
    public Result findby(@RequestBody Brand youfanBrand){
        try {
            Brand youfanBrand1 = brandService.findByid(youfanBrand);

            return Result.success(youfanBrand1, "查询成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("查询失败"+e.getMessage());
        }
    }


    @RequestMapping(method = RequestMethod.POST,value = "update")
    @ResponseBody
    public Result update(@RequestParam(value = "logofile",required = false) MultipartFile logoFile,
                      @RequestParam(value = "logobigfile",required = false) MultipartFile logoBigFile,
                      @RequestParam(value = "brandform" ) String brandform)
    {
        try {

            Brand youfanBrand = JSONObject.parseObject(brandform, Brand.class);
            long id = youfanBrand.getId();
            Brand youfanBrand1 = brandService.findByid(youfanBrand);
            youfanBrand1.setBrandname(youfanBrand.getBrandname());
            youfanBrand1.setShowStatus(youfanBrand.getShowStatus());
            youfanBrand1.setBrandStory(youfanBrand.getBrandStory());
            youfanBrand1.setFirstLetter(youfanBrand.getFirstLetter());
            if ( null != logoFile && !logoFile.isEmpty()) {
                /**
                 * 品牌logo图片处理
                 */
                String oldlogoFileName = logoFile.getOriginalFilename();
                String path = picturepath+"/brand/logo";

                File file = new File(path);
                if (!file.isDirectory()){
                    file.mkdirs();
                }
                String newName = UUID.randomUUID().toString() +oldlogoFileName.substring(oldlogoFileName.indexOf("."), oldlogoFileName.length());
                File newFile = new File(file, newName);
                logoFile.transferTo(newFile);
                youfanBrand1.setLogo("/brand/logo/"+newName);
            }

            if (null != logoBigFile &&!logoBigFile.isEmpty()) {

                /**
                 * 品牌logobig图片处理
                 */
                String oldlogoFileName = logoBigFile.getOriginalFilename();
                String path = picturepath+"/brand/biglogo";

                File file = new File(path);
                if (!file.isDirectory()){
                    file.mkdirs();
                }
                String newName = UUID.randomUUID().toString() +oldlogoFileName.substring(oldlogoFileName.indexOf("."), oldlogoFileName.length());
                File newFile = new File(file, newName);
                logoBigFile.transferTo(newFile);
                youfanBrand1.setBigPic("/brand/biglogo/"+newName);
            }

            brandService.updateOne(youfanBrand1);
            return Result.success("更新成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail("更新失败");
        }
    }

//    /**
//     * 上传
//     * @return
//     */
//    @RequestMapping("/uploadCustomDict")
//    @ResponseBody
//    public Result uploadCustomDict(@RequestParam("file") MultipartFile multipartFile,
//                                   @RequestParam(value = "brandform" ) String brand) throws Exception{
//        if (multipartFile.isEmpty()) {
//            throw new Exception("上传文件不能为空");
//        }
//        brand = brand.toString();
//        String path = "D:\\tmp\\dictdir\\brand";
//
//        File file = new File(path);
//        if (!file.isDirectory()){
//            file.mkdirs();
//        }
//        String oldName = multipartFile.getOriginalFilename();
//        String newName = UUID.randomUUID().toString() +"__" +oldName.substring(oldName.indexOf("."), oldName.length());
////        String newName = oldName+"__"+ UUID.randomUUID().toString();
//        try {
//            File newFile = new File(file, newName);
//            multipartFile.transferTo(newFile);
//        }catch(Exception e){
//            e.printStackTrace();
//        }
//        return Result.success("上传成功");
//    }



}
